import { getSetting } from "@/api/setting";
import { titleControl } from "@/utils";

export default {
    // 开启命名空间
    namespaced: true,  //可以不让这个模块的一些属性和外面的属性混乱(会自动在属性名前面加上)

    state: {    //单一状态树(类似数据data)
        loading: false,
        data: null,
    },
    mutations: {
        setLoading(state, payload) {
            state.loading = payload;
        },
        setData(state, payload) {
            state.data = payload;
        }
    },
    actions: {
        async fetchSetting(ctx) {
            ctx.commit('setLoading', true);
            const result = await getSetting();
            ctx.commit('setData', result);
            ctx.commit('setLoading', false);
            if (result.favicon) {
                // <link rel="shortcut icon " type="images/x-icon" href="./favicon.ico">
                let link = document.querySelector("link[ref='shortcut icon']");
                if (link) {
                    return;
                }
                link = document.createElement("link");
                link.rel = "shortcut icon";
                link.type = "images/x-icon";
                link.href = result.favicon;
                document.querySelector("head").appendChild(link);
            }
            if (result.siteTitle) {
                titleControl.setStoreTitle(result.siteTitle);
            }
        }
    }
}